package com.example.library.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.library.R;
import com.example.library.User;

import java.util.List;

public class UserAdapter extends RecyclerView.Adapter<UserAdapter.UserViewHolder> {
    private List<User> userList;
    private Context context;
    private OnUserActionListener listener;

    public interface OnUserActionListener {
        void onDisableUser(User user);
        void onEnableUser(User user);
        void onAddToBlacklist(User user);
        void onRemoveFromBlacklist(User user);
        void onViewDetails(User user);
    }

    public UserAdapter(Context context, List<User> userList, OnUserActionListener listener) {
        this.context = context;
        this.userList = userList;
        this.listener = listener;
    }

    @NonNull
    @Override
    public UserViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_user, parent, false);
        return new UserViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull UserViewHolder holder, int position) {
        User user = userList.get(position);
        holder.bind(user);
    }

    @Override
    public int getItemCount() {
        return userList.size();
    }

    public void updateData(List<User> newUserList) {
        this.userList = newUserList;
        notifyDataSetChanged();
    }

    class UserViewHolder extends RecyclerView.ViewHolder {
        private TextView tvName, tvStudentId, tvUserType, tvStatus;
        private Button btnDisable, btnEnable, btnBlacklist, btnRemoveBlacklist, btnDetails;

        public UserViewHolder(@NonNull View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tv_user_name);
            tvUserType = itemView.findViewById(R.id.tv_user_type);
            tvStudentId = itemView.findViewById(R.id.tv_student_id);
            tvStatus = itemView.findViewById(R.id.tv_status);
            btnDisable = itemView.findViewById(R.id.btn_disable);
            btnEnable = itemView.findViewById(R.id.btn_enable);
            btnBlacklist = itemView.findViewById(R.id.btn_add_blacklist);
            btnRemoveBlacklist = itemView.findViewById(R.id.btn_remove_blacklist);
            btnDetails = itemView.findViewById(R.id.btn_view_details);
        }

        public void bind(User user) {
            tvName.setText(user.getName());
            tvUserType.setText("身份: " + user.getUserType());
            tvStudentId.setText("学号: " + user.getStudentId());
            switch (user.getStatus()) {
                case 0:
                    tvStatus.setText("状态: 禁用");
                    tvStatus.setTextColor(context.getResources().getColor(android.R.color.holo_red_dark));
                    btnDisable.setVisibility(View.GONE);
                    btnEnable.setVisibility(View.VISIBLE);
                    btnBlacklist.setVisibility(View.VISIBLE);
                    btnRemoveBlacklist.setVisibility(View.GONE);
                    break;
                case 1:
                    tvStatus.setText("状态: 正常");
                    tvStatus.setTextColor(context.getResources().getColor(android.R.color.holo_green_dark));
                    btnDisable.setVisibility(View.VISIBLE);
                    btnEnable.setVisibility(View.GONE);
                    btnBlacklist.setVisibility(View.VISIBLE);
                    btnRemoveBlacklist.setVisibility(View.GONE);
                    break;
                case 2:
                    tvStatus.setText("状态: 黑名单");
                    tvStatus.setTextColor(context.getResources().getColor(android.R.color.holo_red_dark));
                    btnDisable.setVisibility(View.GONE);
                    btnEnable.setVisibility(View.GONE);
                    btnBlacklist.setVisibility(View.GONE);
                    btnRemoveBlacklist.setVisibility(View.VISIBLE);
                    break;
            }
            btnDisable.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onDisableUser(user);
                }
            });
            btnEnable.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onEnableUser(user);
                }
            });
            btnBlacklist.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onAddToBlacklist(user);
                }
            });
            btnRemoveBlacklist.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onRemoveFromBlacklist(user);
                }
            });
            btnDetails.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onViewDetails(user);
                }
            });
        }
    }
} 